package psn.hui.bus.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import psn.hui.bus.domain.Car;
import psn.hui.bus.domain.Rent;
import psn.hui.bus.mapper.CarMapper;
import psn.hui.bus.mapper.RentMapper;
import psn.hui.bus.service.RentService;
import psn.hui.bus.vo.RentVo;
import psn.hui.sys.constant.SysConstant;
import psn.hui.sys.utils.DataGridView;
/**
 * 汽车出租服务接口实现类
 * @author hui
 *
 */
@Service
public class RentServiceImpl implements RentService{
	@Autowired
	private RentMapper rentMapper;
	@Autowired
	private CarMapper carMapper;
	/**
	 * 添加出租单
	 * @param rent
	 * @return
	 */
	public int addRent(Rent rent) {
		int rs = rentMapper.insertSelective(rent);
		Car car = new Car();
		car.setCarnumber(rent.getCarnumber());
		car.setIsrenting(SysConstant.CAR_ISRENTING_TRUE);
		rs += carMapper.updateByPrimaryKeySelective(car);
		return rs;
	}
	
	/**
	 * 修改出租单
	 * @param rent
	 * @return
	 */
	public int updateRent(Rent rent) {
		return rentMapper.updateByPrimaryKeySelective(rent);
	}
	/**
	 * 删除出租单
	 * @param rentid
	 * @return
	 */
	public int deleteRent(String rentid) {
		//改变车辆状态
		Rent rent = rentMapper.selectByPrimaryKey(rentid);
		Car car = new Car();
		car.setCarnumber(rent.getCarnumber());
		car.setIsrenting(SysConstant.CAR_ISRENTING_FALSE);
		carMapper.updateByPrimaryKeySelective(car);
		//删除出租单
		int rs = rentMapper.deleteByPrimaryKey(rentid);
		return rs;
	}
	/**
	 * 	条件查询出租单
	 * @param rentVo
	 * @return
	 */
	public DataGridView queryAllRent(RentVo rentVo) {
		Page<Object> page = PageHelper.startPage(rentVo.getPage(), rentVo.getLimit());
		List<Rent> rentList = rentMapper.queryAllRent(rentVo);
		return new DataGridView(page.getTotal(), rentList);
	}
	
	/**
	 * 根据出租单号查询出租单
	 * @param rentid
	 * @return
	 */
	public Rent queryRentByRentId(String rentid) {
		return rentMapper.selectByPrimaryKey(rentid);
	}
}
